//
//  MYHBViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/30.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "MYHBViewController.h"

#import "MYHBCell.h"
#import "HBTXViewController.h"
@interface MYHBViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *array;
    NSMutableDictionary *dic;
}

@property (nonatomic,strong)UITableView *tableView;

@end

@implementation MYHBViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setBGColor];
    [self showNav:@"我的红包"];
    array = [NSMutableArray array];
    dic = [NSMutableDictionary dictionary];
    
    [self loadData];
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 300)];
    view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    _tableView.tableHeaderView = view;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"01_看图王"] forState:UIControlStateNormal];
    btn.layer.cornerRadius = 40.0;
    btn.layer.masksToBounds = YES;
    [view addSubview:btn];
    [btn makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.centerX);
        make.top.equalTo(view.top).offset(40);
    }];
    
    UIButton *btnY = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnY setImage:[UIImage imageNamed:@"dhxl"] forState:UIControlStateNormal];
    [btnY setTitle:@"2017年" forState:UIControlStateNormal];
    [btnY setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    [view addSubview:btnY];
    [btnY makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(view.right).offset(-16);
        make.top.equalTo(view.top).offset(16);
        
    }];
    
    UILabel *lab1 = [[UILabel alloc] init];
    lab1.text = @"红包余额";
    lab1.font = [UIFont systemFontOfSize:15.0];
    [view addSubview:lab1];
    [lab1 makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.centerX);
        make.top.equalTo(btn.bottom).offset(8);
    }];
    
    UILabel *lab2 = [[UILabel alloc] init];
    NSString *allMoney = [NSString stringWithFormat:@"%.2f",[[dic objectForKey:@"allmoney"] floatValue]];
    lab2.text = allMoney;
    lab2.font = [UIFont boldSystemFontOfSize:42.0];
    lab2.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTX)];
    [lab2 addGestureRecognizer:tap];
    [view addSubview:lab2];
    [lab2 makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.centerX);
        make.top.equalTo(lab1.bottom).offset(0);
    }];
    
    UILabel *lab3 = [[UILabel alloc] init];
    NSString *grabARedEnvelope = [NSString stringWithFormat:@"%.2f",[[dic objectForKey:@"grabARedEnvelope"] floatValue]];
    lab3.text = grabARedEnvelope;
    lab3.textColor = [UIColor darkGrayColor];
    lab3.font = [UIFont systemFontOfSize:24.0];
    [view addSubview:lab3];
    [lab3 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.left).offset(30);
        make.top.equalTo(lab2.bottom).offset(25);
    }];
    
    UILabel *lab4 = [[UILabel alloc] init];
    lab4.text = @"抢到红包";
    lab4.textColor = [UIColor darkGrayColor];
    lab4.font = [UIFont systemFontOfSize:14.0];
    [view addSubview:lab4];
    [lab4 makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(lab3.centerX);
        make.top.equalTo(lab3.bottom).offset(8);
    }];
    
    UILabel *lab5 = [[UILabel alloc] init];
    NSString *movableRedEnvelope = [NSString stringWithFormat:@"%.2f",[[dic objectForKey:@"movableRedEnvelope"] floatValue]];
    lab5.text = movableRedEnvelope;
    lab5.textColor = [UIColor darkGrayColor];
    lab5.font = [UIFont systemFontOfSize:24.0];
    [view addSubview:lab5];
    [lab5 makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(view.right).offset(-30);
        make.top.equalTo(lab2.bottom).offset(25);
    }];
    
    UILabel *lab6 = [[UILabel alloc] init];
    lab6.text = @"活动红包";
    lab6.textColor = [UIColor darkGrayColor];
    lab6.font = [UIFont systemFontOfSize:14.0];
    [view addSubview:lab6];
    [lab6 makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(lab5.centerX);
        make.top.equalTo(lab5.bottom).offset(8);
    }];
    
}

- (void)clickTX{
    HBTXViewController *hbtx = [[HBTXViewController alloc] init];
    NSString *allMoney = [NSString stringWithFormat:@"%.2f",[[dic objectForKey:@"allmoney"] floatValue]];
    hbtx.labMoney.text = allMoney;
    [self.navigationController pushViewController:hbtx animated:YES];
}
#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    MYHBCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MYHBCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0;
}
//红包
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    [parmas setObject:token forKey:@"verifyToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setObject:@"2017" forKey:@"addtimes"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/viewredpackets",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"红包res:%@",responseObject);
        NSString *state = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"status"] integerValue]];
        dic = responseObject;
        if ([state integerValue] == 200) {
            //红包明细
            array = [responseObject objectForKey:@"content"];
        }
        
        [self loadUI];
        [_tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"res:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
